//
//  LRTabBarController.swift
//  WeiboX
//
//  Created by admin on 30/11/2016.
//  Copyright © 2016 iOS_Objective-C. All rights reserved.
//

import UIKit
import SVProgressHUD

class LRTabBarController: UITabBarController {
	
	lazy var arr: [[String: Any]] = {
		var arr = [[String: Any]]()
		arr = [["className": "LRHomeTableVC", "imgName": "tab_bar_home", "title": "Home"],
		       ["className": "LRMessageTableVC", "imgName": "tabbar_message_center", "title": "Message"],
		       ["className": "LRDiscoverTableVC", "imgName": "tabbar_discover", "title": "Discover"],
		       ["className": "LRMeTableVC", "imgName": "tabbar_profile", "title": "Me"]
		]
		return arr
	}()

    override func viewDidLoad() {
        super.viewDidLoad()
		
//		let lrTabBar = LRTabBar()
//
//		lrTabBar.lrDelegate = self
//		
//		lrTabBar.callBack = {[weak self] in
//			print (self!)
//		}
//		
//		setValue(lrTabBar, forKey: "tabBar")
		
        setupUI()
    }
	
	override func loadView() {
		super.loadView()
		let lrTabBar = LRTabBar()
		
		lrTabBar.lrDelegate = self
		
		lrTabBar.callBack = {[weak self] in
//			print (self!)
			
			if !LRUserAccountVM.sharedUserAccount.isLogin {
				SVProgressHUD.showError(withStatus: "请登录后重试")
			}
			// 已经登录
//			let window = UIApplication.shared.windows.last!
//			let composeV = LRComposeV()
//			window.addSubview(composeV)
			if let weakSelf = self {
				let composeV = LRComposeV()
				composeV.show(target: weakSelf)
			}
		}
		
		setValue(lrTabBar, forKey: "tabBar")
	}
	
	func setupUI() {
		
//		let classType = NSClassFromString("WeiboX." + arr[0]) as! UIViewController.Type
//		let vc = classType.init()
//		print(vc)
//		let dic:[String:Any] = arr[1]
//		let str:String = dic["className"] as! String
//		let classType = NSClassFromString("WeiboX." + str) as! UIViewController.Type
//		let vc
//		print(classType.self)

		addChildViewController(childController: LRHomeTableVC(), imgName: "tabbar_home", title: "Home")
		addChildViewController(childController: LRMessageTableVC(), imgName: "tabbar_message_center", title: "Message")
		addChildViewController(childController: LRDiscoverTableVC(), imgName: "tabbar_discover", title: "Discover")
		addChildViewController(childController: LRMeTableVC(), imgName: "tabbar_profile", title: "Me")
	}	
	
	func addChildViewController(childController: UIViewController, imgName: String, title: String) {
		// 图片
		childController.tabBarItem.image = UIImage (named: imgName)
		childController.tabBarItem.selectedImage = UIImage (named: imgName + "_selected")?.withRenderingMode(.alwaysOriginal)
		// 字体
		childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .selected)
		childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 12)], for: .normal)
		// 标题
		childController.title = title
		
		let nav = LRNavController(rootViewController: childController)
		
		addChildViewController(nav)
	}

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension LRTabBarController: LRTabBarDelegate {
	func didSelectedComposeBtn() {
//		print("代理方法")
	}
}
